package com.wnhz.work.yz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.wnhz.work.yz.activity.LoginFirstActivity;
import com.wnhz.work.yz.activity.LoginNewActivity;
import com.wnhz.work.yz.bean.LookDetailsBean;
import com.wnhz.work.yz.bean.SingleAllocationBean;
import com.wnhz.work.yz.bean.UserBean;
import com.wnhz.work.yz.bean.WeiXinBean1;
import com.wnhz.work.yz.bean.WeiXinMessageBean;
import com.wnhz.work.yz.utils.CrashHandler;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/12/3 0003.
 */

public class MyApplication extends MultiDexApplication {
    private static MyApplication instance;

    public static MyApplication getInstance() {
        return instance;
    }
    private static Stack<Activity> activityStack = new Stack<>();

    public String jiji = "0";
    public UserBean userBean;
    public int window_w = -1;
    public int window_h = -1;
    public static List<String> test;
    public boolean flag = false;
    public String PackagePrice;//浴室套餐价格
    public String PackagePrice_GoodsPrice;//浴室套餐价格增加/减少价格
    public String PackagePrice_replacePrice;//浴室替换后的价格
    public SingleAllocationBean singleAllocationBean;//浴室
    public int pricefale = 1;//浴室标志
    public double ysnum ;//加减用
    public double ys_unsetnum;
    public double inputnums;//输入数量保存

    public String kitchenPackagePrice;//厨房套餐价格
    public String kitchenPackagePrice_GoodsPrice;//厨房套餐价格增加/减少
    public String kitchenPackagePrice_replacePrice;//厨房替换之后的价格
    public LookDetailsBean kitchenBean;//厨房
    public double kitnums;
    public int kitchenflag = 1;//厨房标志
    public String replacepeice = "0.00";
    public double inputnums_kit;


    public String GPackagePrice;//厨房套餐价格
    public String GPackagePrice_GoodsPrice;//厨房套餐价格增加/减少
    public String GPackagePrice_replacePrice;//厨房替换之后的价格
    public LookDetailsBean GBean;//厨房
    public double Gnums;
    public int Gflag = 1;//厨房标志
    public String Greplacepeice = "0.00";
    public double inputnums_g;

    public final String WXAPP_ID = "wxb6172005514ce1e7";
    public IWXAPI api;
    public WeiXinBean1 bean1;
    public WeiXinMessageBean bean;
    public int isWXPay = 100;
    public Map<String, Object> sign_map;

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public double getInputnums_g() {
        return inputnums_g;
    }

    public void setInputnums_g(double inputnums_g) {
        this.inputnums_g = inputnums_g;
    }

    public double getInputnums_kit() {
        return inputnums_kit;
    }

    public void setInputnums_kit(double inputnums_kit) {
        this.inputnums_kit = inputnums_kit;
    }

    public double getInputnums() {
        return inputnums;
    }

    public void setInputnums(double inputnums) {
        this.inputnums = inputnums;
    }

    public double getYsnum() {
        return ysnum;
    }

    public void setYsnum(double ysnum) {
        this.ysnum = ysnum;
    }

    public String getPackagePrice() {
        return PackagePrice;
    }

    public void setPackagePrice(String packagePrice) {
        PackagePrice = packagePrice;
    }

    public String getPackagePrice_GoodsPrice() {
        return PackagePrice_GoodsPrice;
    }

    public void setPackagePrice_GoodsPrice(String packagePrice_GoodsPrice) {
        PackagePrice_GoodsPrice = packagePrice_GoodsPrice;
    }

    public String getPackagePrice_replacePrice() {
        return PackagePrice_replacePrice;
    }

    public void setPackagePrice_replacePrice(String packagePrice_replacePrice) {
        PackagePrice_replacePrice = packagePrice_replacePrice;
    }

    public SingleAllocationBean getSingleAllocationBean() {
        return singleAllocationBean;
    }

    public void setSingleAllocationBean(SingleAllocationBean singleAllocationBean) {
        this.singleAllocationBean = singleAllocationBean;
    }

    public int getPricefale() {
        return pricefale;
    }

    public void setPricefale(int pricefale) {
        this.pricefale = pricefale;
    }

    public String getKitchenPackagePrice() {
        return kitchenPackagePrice;
    }

    public void setKitchenPackagePrice(String kitchenPackagePrice) {
        this.kitchenPackagePrice = kitchenPackagePrice;
    }

    public String getKitchenPackagePrice_GoodsPrice() {
        return kitchenPackagePrice_GoodsPrice;
    }

    public void setKitchenPackagePrice_GoodsPrice(String kitchenPackagePrice_GoodsPrice) {
        this.kitchenPackagePrice_GoodsPrice = kitchenPackagePrice_GoodsPrice;
    }

    public String getKitchenPackagePrice_replacePrice() {
        return kitchenPackagePrice_replacePrice;
    }

    public void setKitchenPackagePrice_replacePrice(String kitchenPackagePrice_replacePrice) {
        this.kitchenPackagePrice_replacePrice = kitchenPackagePrice_replacePrice;
    }

    public LookDetailsBean getKitchenBean() {
        return kitchenBean;
    }

    public void setKitchenBean(LookDetailsBean kitchenBean) {
        this.kitchenBean = kitchenBean;
    }

    public int getKitchenflag() {
        return kitchenflag;
    }

    public void setKitchenflag(int kitchenflag) {
        this.kitchenflag = kitchenflag;
    }

    public double getKitnums() {
        return kitnums;
    }

    public void setKitnums(double kitnums) {
        this.kitnums = kitnums;
    }

    public String getReplacepeice() {
        return replacepeice;
    }

    public void setReplacepeice(String replacepeice) {
        this.replacepeice = replacepeice;
    }

    public static void setInstance(MyApplication instance) {
        MyApplication.instance = instance;
    }

    public String getGPackagePrice() {
        return GPackagePrice;
    }

    public void setGPackagePrice(String GPackagePrice) {
        this.GPackagePrice = GPackagePrice;
    }

    public String getGPackagePrice_GoodsPrice() {
        return GPackagePrice_GoodsPrice;
    }

    public void setGPackagePrice_GoodsPrice(String GPackagePrice_GoodsPrice) {
        this.GPackagePrice_GoodsPrice = GPackagePrice_GoodsPrice;
    }

    public String getGPackagePrice_replacePrice() {
        return GPackagePrice_replacePrice;
    }

    public void setGPackagePrice_replacePrice(String GPackagePrice_replacePrice) {
        this.GPackagePrice_replacePrice = GPackagePrice_replacePrice;
    }

    public LookDetailsBean getGBean() {
        return GBean;
    }

    public void setGBean(LookDetailsBean GBean) {
        this.GBean = GBean;
    }

    public double getGnums() {
        return Gnums;
    }

    public void setGnums(double gnums) {
        Gnums = gnums;
    }

    public int getGflag() {
        return Gflag;
    }

    public void setGflag(int gflag) {
        Gflag = gflag;
    }

    public String getGreplacepeice() {
        return Greplacepeice;
    }

    public void setGreplacepeice(String greplacepeice) {
        Greplacepeice = greplacepeice;
    }

    public void initTest() {
        test = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            test.add("333." + i);
        }
    }

//    public static RefWatcher getRefWatcher(Context context) {
//        MyApplication application = (MyApplication) context.getApplicationContext();
//        return application.refWatcher;
//    }
//
//    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        initTest();
        instance = this;
        x.Ext.init(this);
        UMShareAPI.get(this);
        MultiDex.install(this);
        ZXingLibrary.initDisplayOpinion(this);
        initImageLoader();
//        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        api = WXAPIFactory.createWXAPI(this, WXAPP_ID);

        MQConfig.init(this, "a5aa98aaedaeadbd636625af8b35cfe1", new

                OnInitCallback() {
                    @Override
                    public void onSuccess(String clientId) {
                    }

                    @Override
                    public void onFailure(int code, String message) {
                    }
                }

        );

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
//        refWatcher = LeakCanary.install(this);

        //webView
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            //    Log.d("app", " onViewInitFinished is " + arg0);
                System.out.println(" onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(),  cb);

    }

    {
        PlatformConfig.setWeixin("wxb6172005514ce1e7", "dadad813cd07a6656995b891285192c4");
        PlatformConfig.setQQZone("1106010082", "iwePG5xPphFeayUX");
    }

    public ImageOptions shopoptions = new ImageOptions.Builder()
            //设置使用缓存
            .setUseMemCache(true)
            //设置显示圆形图片
            .setCircular(false)
            //设置支持gif
            .setIgnoreGif(false)
            .setAutoRotate(true)
            .build();

    /**
     * 配置ImageLoader
     */
    public void initImageLoader() {
        options = new DisplayImageOptions.Builder()// 开始构建, 显示的图片的各种格式
                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                .cacheInMemory(true)// 开启内存缓存
                .cacheOnDisk(true) // 开启硬盘缓存
                .displayer(new RoundedBitmapDisplayer(20))// 是否设置为圆角，弧度为多少；避免使用RoundedBitmapDisplayer.他会创建新的ARGB_8888格式的Bitmap对象；
                .displayer(new FadeInBitmapDisplayer(100))// 是否图片加载好后渐入的动画时间
                .displayer(new SimpleBitmapDisplayer())// 正常显示一张图片　
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型;使用.bitmapConfig(Bitmap.config.RGB_565)代替ARGB_8888;
                .considerExifParams(true)// 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY)// 缩放级别
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//这两种配置缩放都推荐
                .build();// 构建完成（参数可以不用设置全，根据需要来配置）

        //  --------------- ImageLoaderConfiguration配置----------------------
        config = new ImageLoaderConfiguration.Builder(getInstance())// 开始构建 ,图片加载配置
                .threadPriority(Thread.NORM_PRIORITY - 2)// 设置线程优先级
                .threadPoolSize(3)// 线程池内加载的数量 ;减少配置之中线程池的大小，(.threadPoolSize).推荐1-5；
                .denyCacheImageMultipleSizesInMemory()// 设置加载的图片有多样的
                .tasksProcessingOrder(QueueProcessingType.LIFO)// 图片加载任务顺序
                .memoryCache(new WeakMemoryCache())//使用.memoryCache(new WeakMemoryCache())，不要使用.cacheInMemory();
                .memoryCacheExtraOptions(480, 800) // 即保存的每个缓存文件的最大长宽
                .memoryCacheSizePercentage(60)// 图片内存占应用的60%；
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//使用HASHCODE对UIL进行加密命名
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5 加密
                .diskCacheSize(50 * 1024 * 1024) // 缓存设置大小为50 Mb
//                .diskCache(new UnlimitedDiskCache())// 自定义缓存路径
                .diskCacheFileCount(100) // 缓存的文件数量
                .denyCacheImageMultipleSizesInMemory()// 自动缩放
                .imageDownloader(new BaseImageDownloader(getInstance(), 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
                .memoryCacheExtraOptions(480, 800)//设置缓存图片时候的宽高最大值，默认为屏幕宽高;保存的每个缓存文件的最大长宽
//                .defaultDisplayImageOptions(options)// 如果需要打开缓存机制，需要自己builde一个option,可以是DisplayImageOptions.createSimple()
//                .writeDebugLogs()
                .build(); // Remove for release app
        ImageLoader.getInstance().init(config);
    }

    private ImageLoaderConfiguration config;
    public DisplayImageOptions options;

    //往栈中添加activity
    public void addActivity(Activity activity) {
        if (activity != null) {
            activityStack.add(activity);
        }
    }

    //从栈中移出activity
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }

    //依次销毁activity
    private void finishActivity() {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i) != null && !activityStack.get(i).isFinishing()) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    //跳转到登录界面
    public void gotoLoginActivity() {
        finishActivity();

        Intent intent = new Intent();
        intent.setClass(this, LoginFirstActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //跳转到登录界面
    public void gotoLoginNewActivity() {
        finishActivity();

        Intent intent = new Intent();
        intent.setClass(this, LoginNewActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    //退出登录
    public void exit(Activity activity) {
        finishActivity();
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
        }
    }

}
